package com.bw.users.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.users.dao.UserDao;
import com.bw.users.entity.UserEntity;
import com.bw.users.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserEntity login(String userPassowrd, String userPhone) {
        UserEntity login = userDao.login(userPassowrd, userPhone);
        String s = redisTemplate.opsForValue().get(login.getUserId().toString());
        if (s!=null){
            redisTemplate.opsForValue().set(login.getUserId().toString(), login.getUserName(),30, TimeUnit.MICROSECONDS);
        }

        return login;
    }

}